package com.mayikt.zuul.handler.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Console;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mayikt.zuul.feign.VerificaCodeServiceFeign;
import com.mayikt.zuul.handler.BaseHandler;
import com.mayikt.zuul.handler.GatewayHandler;
import com.mayikt.zuul.mapper.entity.MeiteBlacklist;
import com.mayikt.zuul.service.IMeiteBlacklistService;
import com.mayikt.zuul.util.RequestUtil;
import com.netflix.zuul.context.RequestContext;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;

@Service
@Slf4j
public class BlacklistHandler extends BaseHandler implements GatewayHandler {
    @Autowired
    private IMeiteBlacklistService meiteBlacklistService;

    @Override
    public void service(RequestContext rc, String ipAddress, HttpServletResponse response) {
        QueryWrapper<MeiteBlacklist> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MeiteBlacklist::getIpAddress, RequestUtil.getIpAddr(rc.getRequest())).eq(MeiteBlacklist::getAvailability,2);
        MeiteBlacklist meiteBlacklist = meiteBlacklistService.getOne(queryWrapper);
        if(!BeanUtil.isEmpty(meiteBlacklist)){
            Console.log("Your ip has been blocked!");
            this.resultError(rc,"Your ip has been blocked!");
        }
        if (!BeanUtil.isEmpty(this.nextGatewayHandler)){
            this.nextGatewayHandler.service(rc,ipAddress,response);
        }
    }

}
